package com.yunkaweilai.android.view.dialog.integral;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.utils.CornerUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.github.lazylibrary.util.PreferencesUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.interfaces.DialogPayInterface;
import com.yunkaweilai.android.model.SendModel;
import com.yunkaweilai.android.model.integral.IntegralGoodListModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;

import java.util.Map;

/**
 * 积分兑换商品
 * Created by Administrator on 2016/12/22 0022.
 */

public class IntegralRightDialog extends BaseDialog<IntegralRightDialog> {
    ImageView idImgClose;
    private TextView idTvClose;
    private TextView idTvRight;
    private TextView idTvNoW;
    private TextView idTvUse;
    private TextView idTvContent;
    private DialogPayInterface dialogPayInterface;
    private String nowPoints;
    private String usePoints;
    private Map<String, IntegralGoodListModel> map;
    private CheckBox idCheckSms;
    private CheckBox idCheckVx;
    private CheckBox idCheckPrint;
    private boolean sms = false;
    private boolean wechat = false;
    private boolean print = false;
    private Context context;

    public IntegralRightDialog(Context context, DialogPayInterface dialogPayInterface, String nowPoints, String usePoints, Map<String, IntegralGoodListModel> map,boolean sms , boolean wechat, boolean print) {
        super(context);
        this.context = context;
        this.dialogPayInterface = dialogPayInterface;
        this.nowPoints = nowPoints;
        this.usePoints = usePoints;
        this.map = map;
        this.sms = sms;
        this.wechat = wechat;
        this.print = print;
    }

    @Override
    public View onCreateView() {
        widthScale(0);
        //showAnim(new Swing());
        //dismissAnim(this, new ZoomOutExit());
        View inflate = View.inflate(mContext, R.layout.dialog_integral_right, null);
        idImgClose = (ImageView) inflate.findViewById(R.id.id_img_close);
        idTvRight = ((TextView) inflate.findViewById(R.id.id_tv_right));
        idTvClose = (TextView) inflate.findViewById(R.id.id_tv_close);
        idTvNoW = ((TextView) inflate.findViewById(R.id.id_tv_now_integral));
        idTvUse = ((TextView) inflate.findViewById(R.id.id_tv_use_integral));
        idTvContent = ((TextView) inflate.findViewById(R.id.id_tv_integral_content));
        idCheckSms = ((CheckBox) inflate.findViewById(R.id.id_check_sms));
        idCheckVx = ((CheckBox) inflate.findViewById(R.id.id_check_vx));
        idCheckPrint = ((CheckBox) inflate.findViewById(R.id.id_check_print));
        initView();
        if (sms){
            idCheckSms.setAlpha((float)1);
            idCheckSms.setClickable(true);
        }else {
            idCheckSms.setAlpha((float)0.3);
            idCheckSms.setClickable(false);
        }
        if (wechat){
            idCheckVx.setAlpha((float)1);
            idCheckVx.setClickable(true);
        }else {
            idCheckVx.setAlpha((float)0.3);
            idCheckVx.setClickable(false);
        }
        if (print){
            idCheckPrint.setAlpha((float)1);
            idCheckPrint.setClickable(true);
        }else {
            idCheckPrint.setAlpha((float)0.3);
            idCheckPrint.setClickable(false);
        }

        initGetTuisong();
        //设置背景颜色，和背景的透明圆弧角度
        inflate.setBackgroundDrawable(
                CornerUtils.cornerDrawable(Color.parseColor("#f8f9fa"), dp2px(10)));

        return inflate;
    }

    private void initView() {
        boolean aBoolean = PreferencesUtils.getBoolean(getContext(), CommonConstants.POINTS_DAYING, false);
        idCheckPrint.setChecked(aBoolean);

        Drawable drawable = getContext().getResources().getDrawable(R.drawable.selector_checkbox_style);
        //设置drawable对象的大小
        drawable.setBounds(0, 0, 40, 40);
        Drawable drawable2 = getContext().getResources().getDrawable(R.drawable.selector_checkbox_style);
        //设置drawable对象的大小
        drawable2.setBounds(0, 0, 40, 40);
        Drawable drawable3 = getContext().getResources().getDrawable(R.drawable.selector_checkbox_style);
        //设置drawable对象的大小
        drawable3.setBounds(0, 0, 40, 40);
        idCheckSms.setCompoundDrawables(drawable, null, null, null);
        idCheckVx.setCompoundDrawables(drawable2, null, null, null);
        idCheckPrint.setCompoundDrawables(drawable3, null, null, null);
    }


    private void initGetTuisong() {
        HttpRequest.postUrl(Api.GET_SEND_STATUS)
                .addParams("send_view", "Points")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(getContext(), json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            SendModel sendModel = gson.fromJson(json, SendModel.class);
                            if (sendModel != null) {
                                if ("1".equals(sendModel.getData().getInfo().getSend_sms_status())&&sms) {
                                    idCheckSms.setChecked(true);
                                }
                                if ("1".equals(sendModel.getData().getInfo().getSend_wechat_status())&&wechat) {
                                    idCheckVx.setChecked(true);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                    }
                });
    }

    @Override
    public void setUiBeforShow() {

        idTvNoW.setText("现有积分：" + nowPoints);
        idTvUse.setText("使用积分：" + usePoints);
        String str = "";
        for (String key : map.keySet()) {
            IntegralGoodListModel listBean = map.get(key);
            str = str + listBean.getGift_name() + "(" + listBean.getPoints_price() + "*" + (int) listBean.getBuy_num() + ")，";
        }
        str = str.substring(0, str.length() - 1);
        idTvContent.setText("兑换礼品：" + str);
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogPayInterface.payMoney();
                dismiss();
            }
        });
        idTvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        idCheckPrint.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                PreferencesUtils.putBoolean(getContext(), CommonConstants.POINTS_DAYING, isChecked);
            }
        });
        idCheckSms.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                saveSendStatus("send_sms", isChecked, idCheckSms);
            }
        });
        idCheckVx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                saveSendStatus("send_wechat", isChecked, idCheckVx);
            }
        });
    }

    private void saveSendStatus(String sendType, boolean status, final CheckBox checkBox) {
        HttpRequest.postUrl(Api.SAVE_SEND_STATUS)
                .addParams("send_status", status ? "1" : "0")
                .addParams("send_type", sendType)
                .addParams("send_view", "Points")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context,json);
                        if (!checkCode) {
                            checkBox.setChecked(false);
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }

}
